<template>
	<div class="bargain-info">
		<bargain-info-container
				v-if="basicInfo"
				:joiner="joiner"
				:basicInfo="basicInfo"
				:info="info"
				:helps="helps"
				:nowPrice="nowPrice"
				:allBargain="allBargain"
				@getBargainInfo="getBargainInfo"
				@dialogShow="showDialog"
		/>
		<bargain-info-helps v-if="basicInfo" :info="info" :helps="helps"/>
		<go-back/>
		<share-dialog
				v-if="basicInfo"
				v-show="dialogShow"
				:dialogShow="dialogShow"
				@close="closeDialog"
				@share="shareBargain"
		/>
	</div>
</template>

<script>
	import bargainInfoContainer from '#/bargainInfo/container';
	import bargainInfoHelps from '#/bargainInfo/bargainInfoHelps';
	import shareDialog from '#/bargainInfo/shareDialog';
	import GoBack from '#/Public/GoBack';
	import Bargain from '@/services/bargain';
	import Goods from '@/services/goods';
	import Share from '@/services/share';

	const _bargain = new Bargain();
	const _goods = new Goods();
	const _share = new Share();
	export default {
		name: "bargainInfo",
		data() {
			return {
				joiner: null, // 发起砍价用户
				info: null, // 砍价信息
				helps: [], // 帮助名单
				basicInfo: null, // 商品信息
				dialogShow: true // 弹出层显示隐藏
			}
		},
		created() {
			this.getBargainInfo(); // 获取砍价信息
		},
		computed: {
			// 计算当前价格
			nowPrice() {
				let price = this.info.curPrice;
				for (let i = 0; i < this.helps.length; i++) {
					price -= this.helps[i].cutPrice
				}
				return price;
			},
			// 计算当前砍价总金额
			allBargain() {
				let price = 0;
				for (let i = 0; i < this.helps.length; i++) {
					price += this.helps[i].cutPrice
				}
				return price;
			}
		},
		methods: {
			// 获取砍价信息
			getBargainInfo() {
				let kjid = this.$route.params.id; // 砍价id
				let joiner = this.$route.params.userId; // 用户id
				let params = { // 参数对象
					kjid,
					joiner
				};
				// 获取砍价信息
				_bargain.getBargainInfo(params).then(res => {
					this.joiner = res.data.joiner; // 发起砍价用户
					this.info = res.data.kanjiaInfo; // 砍价信息
					this.helps = res.data.helps; // 帮助列表
				}).then(() => {
					this.getGoodsDetail(); // 获取商品信息
				})
			},
			getGoodsDetail() {
				let id = this.info.goodsId;
				_goods.getGoodDetail(id).then(res => {
					console.log(res);
					this.basicInfo = res.data.basicInfo;
				})
			},
			closeDialog() {
				this.dialogShow = false;
			},
			showDialog() {
				this.dialogShow = true;
			},
			shareBargain() {
				const _this = this;
				_share.shareBargain().then(res => {
					console.log(res);
					wx.config({
						debug: true,
						appId: res.appId,
						timestamp: res.timestamp,
						signature: res.sign,
						nonceStr: res.nonceStr,
						jsApi: res.jsApi,
						jsApiList: [
							// 所有要调用的 API 都要加到这个列表中
							'onMenuShareTimeline',    // 分享到朋友圈接口
							'onMenuShareAppMessage'
						]
					});
					_this.$message('再等会儿吧');
					wx.ready(function(){
						// 分享到朋友圈
						wx.onMenuShareTimeline({
							title: _this.basicInfo.name, // 商品标题
							desc: '我在晨光-严选商城发起了一个砍价，是兄弟就快来砍我！', // 商品描述
							link: "http://www.wyunfei.com/pay1.html", // 好友从朋友圈点进去的页面
							imgUrl: _this.basicInfo.pic, // 商品图片
							success: function (res) {
								_this.$alert({
									type: 'success',
									message: '分享成功！'
								})
							}
						});
						// 分享给好友
						wx.onMenuShareAppMessage({
							title: _this.basicInfo.name, // 商品标题
							desc: '我在晨光-严选商城发起了一个砍价，是兄弟就快来砍我！', // 商品描述
							link: "http://www.wyunfei.com/pay1.html",
							imgUrl: _this.basicInfo.pic,
							success: function (res) {
								_this.$alert({
									type: 'success',
									message: '分享成功！'
								})
							},
						});
						_this.$alert('试试分享吧')
					})
				})
			}
		},
		components: {
			bargainInfoContainer,
			bargainInfoHelps,
			shareDialog,
			GoBack
		}
	}
</script>
